package com.wyait.manage.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyait.manage.dao.LogSystemMapper;
import com.wyait.manage.pojo.LogSystem;
import com.wyait.manage.pojo.User;
import com.wyait.manage.utils.PageDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  日志系统LogSystemServiceImpl
 */
@Service
public class LogSystemServiceImpl implements LogSystemService{

    @Autowired(required = false)
    private LogSystemMapper logSystemMapper;

    @Override
    public PageDataResult getLogSystemList(LogSystem logSystem, Integer page, Integer limit) {
        PageDataResult pdr = new PageDataResult();
        PageHelper.startPage(page, limit);
        List<LogSystem> logSystemList = logSystemMapper.getLogSystemList(logSystem);
        // 获取分页查询后的数据
        PageInfo<LogSystem> pageInfo = new PageInfo<>(logSystemList);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(logSystemList);
        return pdr;
    }

    @Override
    public void setLog(User currentUser, Map<String, String> map) {
        LogSystem logSystem = new LogSystem();

        logSystem.setUserId(currentUser.getId());
        logSystem.setUserName(currentUser.getUsername());
        logSystem.setNewTime(new Date());
        logSystem.setType(Integer.valueOf(map.get("type")));

        String oldName = map.get("oldName");
        //旧名称不为空,添加更新日志
        if(oldName != null){
            //新旧名称一样
            if(oldName.equals(map.get("newName"))){

                logSystem.setContent("管理员（"+currentUser.getUsername()+"）修改了"+ map.get("logName") +"《" + oldName +"》的内容");
            }
            //新旧名称不一样
            else{
                logSystem.setContent("管理员（"+currentUser.getUsername()+"）修改了"+ map.get("logName") + "《" + oldName + "》的内容,修改后名称《"+ map.get("newName") +"》");
            }

        }
        //旧名称不存在，添加新增日志
        else{
            logSystem.setContent("管理员（"+currentUser.getUsername()+"）新增了"+ map.get("logName") +"，命名为："+ map.get("newName"));
        }
        logSystemMapper.insert(logSystem);
    }



    @Override
    public void setDelLog(User currentUser,String logName, String delName,Integer type) {
        LogSystem logSystem = new LogSystem();

        logSystem.setUserId(currentUser.getId());
        logSystem.setUserName(currentUser.getUsername());
        logSystem.setNewTime(new Date());
        logSystem.setType(type);

        logSystem.setContent("管理员（" + currentUser.getUsername() + "）删除了"+ logName+"《" + delName +"》");

        logSystemMapper.insert(logSystem);
    }

    @Override
    public String delLogSave10() {
        List<LogSystem> logSystemList = logSystemMapper.findLogLimit10();

        //封装最新的10条日志id列表
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < logSystemList.size(); i++) {
            ids.add(logSystemList.get(i).getId());
        }

        logSystemMapper.delLog(ids);

        return "ok";
    }
}
